// 获取product
var shop = document.querySelector("#shopping");

function getUrl(url) {
    var urlArr = url.split("?")[1];
    // console.log(urlArr);
    urlArr = urlArr.split("&");
    // console.log(urlArr);

    var obj = {};
    urlArr.forEach(item => {
        // console.log(item);
        var itemArr = item.split("=");
        // console.log(itemArr);

        obj[itemArr[0]] = itemArr[1];
    });
    // console.log(obj);
    return obj;
}

(function(){
    var urlstring = decodeURI(location.href);
    let regExp = /(?<=imgurl=)[^'"]*/;
    console.log(urlstring);

    var pObj = getUrl(urlstring);
    console.log(pObj);

    const xhr = new XMLHttpRequest();
    xhr.open("get","http://chst.vip:1234/api/getproductlist?categoryid="+pObj.categoryid,true);
    xhr.send();
    xhr.onreadystatechange = function(){
         if(xhr.readyState === 4 && xhr.status === 200){
            var res = JSON.parse(xhr.responseText);
            console.log(res.result);
            var rex = res.result;
            var ele = "";
            rex.forEach(item => {
                console.log(item);
                ele += `

                <li class="photo" productid="${item.productId}">

                <div class="photo_left" productid="${item.productId}">
                ${item.productImg}
                </div>

                <div class="photo_right" productid="${item.productId}">

                    <div class="photo_right_top" productid="${item.productId}">
                        <span productid="${item.productId}"> ${item.productName}</span>
                        <br>
                        <span productid="${item.productId}">${item.productPrice}</span>
                    </div>

                    <div class="photo_right_bottom">
                        <span productid="${item.productId}">${item.productQuote}</span>
                        <span>
                            <img src="../assets/images/消息.png" alt="">
                            <p>${item.productCom}</p>
                        </span>
                    </div>
            </li>
                `
            })
            shop.innerHTML = ele;
         }
        
    }
// 

    // 点击事件
var shop= document.querySelector("#shopping")
shop.onclick = function(event){
    var e = event;
    console.log(e);
    var target = e.target;
    console.log(target);
    var id = target.getAttribute("productid");
    console.log(id);
    location.href="../html/商品详情功能界面.html?productid="+id
}
$("gotop").click(function(){
    $("body,html").animate({scrollTop:0},1000);
})
})();


